﻿#include "KeyboardHandler.h"
#include "Utils.h"
#include "WindowsService.h"

#include <tchar.h>
#include <windows.h>
#include <winuser.h>

// 修改为使用 TCHAR 类型
int _tmain(int argc, TCHAR *argv[]) {
  // 处理安装/卸载逻辑
  if (argc > 1) {
    ServiceManager sm(SERVICE_NAME);
    // 使用 _tcscmp 进行字符串比较
    if (_tcscmp(argv[1], _T("/install")) == 0) {
      if (sm.InstallService()) {
        // 使用 _tprintf 输出信息
        LOG(_T("Service installed successfully.\n"));
      } else {
        LOG(_T("Failed to install service.\n"));
      }
      return 0;
    } else if (_tcscmp(argv[1], _T("/uninstall")) == 0) {
      if (sm.UninstallService()) {
        LOG(_T("Service uninstalled successfully.\n"));
      } else {
        LOG(_T("Failed to uninstall service.\n"));
      }
      return 0;
    } else {
      PrintError(_T("Invalid command"), GetLastError());
    }
    return -1;
  }
  LOG(_T("service start"));
  // 服务运行逻辑
  SERVICE_TABLE_ENTRY serviceTable[] = {
      {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)Service::ServiceMain},
      {nullptr, nullptr}};
  if (!StartServiceCtrlDispatcher(serviceTable)) {
    PrintError(_T("StartServiceCtrlDispatcher"), GetLastError());
    return 1;
  }
  return 0;
}
